package com.skwyb.form.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skwyb.form.bean.dto.SysRole;
import com.skwyb.form.bean.req.IdReq;
import com.skwyb.form.bean.req.admin.GetRoleListReq;
import com.skwyb.form.bean.req.admin.SaveRoleReq;
import com.skwyb.form.core.BusinessException;
import com.skwyb.form.core.ObjectResult;
import com.skwyb.form.service.admin.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/list")
    public Page<SysRole> list(@Valid GetRoleListReq req) {
        SysRole record = new SysRole();
        BeanUtils.copyProperties(req, record);
        Page<SysRole> page = new Page<>(req.getPage(), req.getRows());
        roleService.list();
        return roleService.page(page, Wrappers.lambdaQuery(record));
    }

    @PostMapping("/get")
    public ObjectResult get(@Valid IdReq req) {
        SysRole record = new SysRole();
        BeanUtils.copyProperties(req, record);
        return new ObjectResult(roleService.selectRole(record));
    }

    @PostMapping("/save")
    public ObjectResult save(@Valid SaveRoleReq req) {
        return new ObjectResult(roleService.saveRole(req));
    }

    @PostMapping("/delete")
    public ObjectResult delete(@Valid IdReq req) throws BusinessException {
        return new ObjectResult(roleService.removeById(req.getId()));
    }
}
